# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader.processors import MapCompose, Join
from scrapy.loader import ItemLoader
from robot.items import RobotItem


class RarbgSpider(scrapy.Spider):
    name = 'rarbg'
    allowed_domains = ['rarbg.io']
    start_urls = ['http://rarbg.io/']

    def parse(self, response):
        l = ItemLoader(item=RobotItem(), response=response)
        l.add_xpath('title', '//h1/text()',
                    MapCompose(unicode.strip, unicode.title))
        l.add_xpath('price', './/h2/text()',
                    MapCompose(lambda i: i.replace(',', ''), float), re='[,.0-9]+')
        l.add_xpath('description', '//div[1]/text()',
                    MapCompose(unicode.strip), Join())
